<?php

namespace app\admin\controller;

use app\admin\model\Role;
use app\common\enums\StatusEnum;
use think\facade\Request;
use app\admin\validate\User AS UserValidate;
use app\admin\model\User AS UserModel;
use app\admin\validate\UserRole;

class User extends Base
{
    public function index()
    {
        $where = input('');
        if (empty($where)) {
            $where = [
                'user_name' => '',
                'mobile' => '',
                'status' => -1,
                'role' => ''
            ];
        }
        $model = UserModel::getList($where);
        return view('index', [
            'model' => $model['model'],
            'count' => $model['count'],
            'status' => StatusEnum::$listExplain,
            'where' => $where
        ]);
    }

    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');

            // 检测变量
            $validate = new UserValidate();

            if (isset($data['id']) && ($data['id'] > 0)) {
                if (!$validate->scene('edit')->check($data)) {
                    return json(self::sendResult(102, [], $validate->getError()));
                }
                
                if ($data['password'] && !$data['rePassword']) {
                    return json(self::sendResult(102, [], '请输入确认密码'));
                }
            } else {
                if (!$validate->check($data)) {
                    return json(self::sendResult(102, [], $validate->getError()));
                }
            }

            $return = UserModel::saveOne($data);
            $code = $return === true ? self::$okCode : 602;
            return json(self::sendResult($code, [
                'url' => url('index'),
            ]));
        } else {
            $id = input('id');
            $model = [];
            $title = '添加管理员';
            if ($id > 0) {
                $model = UserModel::get($id);
                $title = '编辑管理员';
            }
            return view('form', [
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'title' => $title
            ]);
        }
    }

    public function assignment()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');
         
            // 检测变量
            $validate = new UserRole();
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }

            $res = UserModel::assignment($data);
            return json(self::sendResult($res['code'], [
                'url' => url('index'),
            ], $res['msg']));
        } else {
            $id = input('id');
            $model = [];
            if ($id > 0) {
                $model = UserModel::get($id);
            }

            $role = Role::getAllEnabled(false, $id);
            return view('assignment', [
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'role' => $role
            ]);
        }
    }

    public function delete($id)
    {
        UserModel::deleteOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }
}